#include "bstring.h"

#include "bool.h"

#include <QtCore/QRegExp>
#include <QtCore/QVariant>

using namespace core;

const TypeName BasicString::NAME = Type::demangledName(
    typeid(BasicString).name());

BasicString::BasicString(const QString& value) : ValueType(),
    m_value(value)
{
    m_operations.insert(OPERATION_EQUALTO, OP(Bool::NAME));
    m_operations.insert(OPERATION_NEQUALTO, OP(Bool::NAME));
}

BasicString::~BasicString()
{

}

void BasicString::setValue(const QVariant& value)
{
    if (value.canConvert(QVariant::String))
    {
        m_value = value.toString().remove("\"");
    }
}

QVariant BasicString::getValue() const
{
    return QVariant(m_value);
}

bool BasicString::operator ==(const Type& type) const
{
    const BasicString* other = dynamic_cast<const BasicString *>(&type);
    return other != 0 ? m_value == other->m_value : false;
}

bool BasicString::operator !=(const Type& type) const
{
    const BasicString* other = dynamic_cast<const BasicString *>(&type);
    return other != 0 ? m_value != other->m_value : true;
}

bool BasicString::canConvert(const TypeValue& value) const
{
    QRegExp regex(keywordMask());

    return regex.exactMatch(value);
}

ValueKeywordMask BasicString::keywordMask() const
{
    return "\"\\w*\"";
}

Type* BasicString::copy() const
{
    BasicString* newInstance = new BasicString(m_value);
    return newInstance;
}
